
package fr.umlv.m2.uwp2011.web.client.professor;

import java.util.LinkedList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TabBar;
import com.google.gwt.user.client.ui.VerticalPanel;

import fr.umlv.m2.uwp2011.service.matiere.DisplayCursus;
import fr.umlv.m2.uwp2011.service.matiere.DisplayMatiere;
import fr.umlv.m2.uwp2011.service.matiere.ICursusService;
import fr.umlv.m2.uwp2011.service.matiere.ICursusServiceAsync;
import fr.umlv.m2.uwp2011.service.matiere.IMatiereService;
import fr.umlv.m2.uwp2011.service.matiere.IMatiereServiceAsync;
import fr.umlv.m2.uwp2011.web.client.Session;

public class ProfessorPlanningWorks extends Composite {
	/**
	 * TD work type.
	 */
	public static final String TD = "td";
	/**
	 * TP work type.
	 */
	public static final String TP = "tp";
	/**
	 * Project work type.
	 */
	public static final String PROJECT = "project";

	/**
	 * The list of cursus. 
	 */
	private final ListBox cursus;
	/**
	 * The TabBar of course.
	 */
	private final TabBar bar;

	/**
	 * The page panel.
	 */
	private VerticalPanel pagePanel;
	/**
	 * The main panel.
	 */
	private VerticalPanel page;
	/**
	 * rpc cursus for collecting informations about cursus.
	 */
	private final ICursusServiceAsync cursusService;
	
	/**
	 * rpc cursus for collecting informations about matiere.
	 */
	private final IMatiereServiceAsync matiereService;

	/**
	 * list of cursus.
	 */
	private final List<DisplayCursus> listCursus;
	
	private Long currentCursusId;
	
	public ProfessorPlanningWorks(final Session session) {
		/****************************** Widgets Creation ******************************/
		//this.session = session;
		this.cursusService = GWT.create(ICursusService.class);
		this.matiereService = GWT.create(IMatiereService.class);
		this.listCursus = new LinkedList<DisplayCursus>();

		this.cursus = new ListBox();
		this.bar = new TabBar();

		this.pagePanel = new VerticalPanel();
		this.page = new VerticalPanel();

		/*************************** Widgets initialization ***************************/
		this.cursusService.getListCursusByUser(session.getUser().getLogin(), new AsyncCallback<List<DisplayCursus>>() {
			
			@Override
			public void onFailure(Throwable caught) {}
			
			@Override
			public void onSuccess(List<DisplayCursus> result) {
				for(DisplayCursus dcursus : result) {
					listCursus.add(dcursus);
					cursus.addItem(dcursus.getLibelle());
				}
				currentCursusId = listCursus.get(0).getId();
			}
		});
		
		this.matiereService.getByUserAndCursus(session.getUser().getLogin(), currentCursusId, new AsyncCallback<List<DisplayMatiere>>() {

			@Override
			public void onFailure(Throwable caught) {
				//Window.alert("session login : " + session.getUser().getLogin() + " / currentCursusId : " + currentCursusId);
			}

			@Override
			public void onSuccess(List<DisplayMatiere> result) {
				for(DisplayMatiere dmatiere : result) {
					bar.addTab(dmatiere.getLibelle());
				}
			}
		});

		/******************************** Widgets style *******************************/


		/******************************** Page building *******************************/

		this.pagePanel.add(this.cursus);
		this.pagePanel.add(this.bar);

		this.page.add(this.pagePanel);
		initWidget(this.page);
//
//		/******************************* Action handler *******************************/
//		this.cursus.addChangeHandler(new ChangeHandler() {
//			@Override
//			public void onChange(ChangeEvent event) {
//				//currentCursus = listCursus.get(cursus.getSelectedIndex());
//				//bar.selectTab(0);
//				//coursService.getByMatiereID(currentMatiere.getId(), displayCoursCallBack());
//			}
//		});
//
//		this.bar.addSelectionHandler(new SelectionHandler<Integer>() {
//			@Override
//			public void onSelection(SelectionEvent<Integer> event) {
//
//			}
//		});
	}
}
